package com.clayecloud.common.client.services.Impl;

import cn.hutool.system.UserInfo;
import com.clayecloud.common.client.services.UserServices;
import com.clayecloud.common.entity.User;
import com.clayecloud.common.entity.UserRole;
import com.clayecloud.common.entity.Userinfo;
import com.clayecloud.common.mappers.UserMapper;
import com.clayecloud.common.mappers.UserRoleMapper;
import com.clayecloud.common.mappers.UserinfoMapper;
import com.clayecloud.common.utils.MessageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServicesImpl implements UserServices {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private UserinfoMapper userinfoMapper;

    @Override
    public MessageUtil insert(User record) {
        return userMapper.insert(record) > 0 ? MessageUtil.success("注册成功") : MessageUtil.fail("注册失败");
    }

    @Override
    public MessageUtil deleteByPrimaryKey(Integer userId) {
        return userMapper.deleteByPrimaryKey(userId) > 0 ? MessageUtil.success("注销账户成功") : MessageUtil.fail("注销账户失败");
    }

    @Override
    public MessageUtil insertSelective(User record) {
        return userMapper.insertSelective(record) > 0 ? bindUser(record) : MessageUtil.fail("注册失败").add(record);
    }

    @Override
    public MessageUtil selectByPrimaryKey(Integer userId) {
        return MessageUtil.success("查询成功").add(userMapper.selectByPrimaryKey(userId));
    }

    @Override
    public MessageUtil updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record) > 0 ? MessageUtil.success("更改成功") : MessageUtil.fail("更改失败");
    }

    @Override
    public MessageUtil updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record) > 0 ? MessageUtil.success("更改成功") : MessageUtil.fail("更改失败");
    }

    @Override
    public MessageUtil selectAll() {
        return MessageUtil.success("查询成功").add(userMapper.selectAll());
    }

    @Override
    public MessageUtil selectByMail(String usermail) {
        return MessageUtil.success("查询成功").add(userMapper.selectByMail(usermail));
    }

    @Override
    public MessageUtil selectByPhone(String userphone) {
        return MessageUtil.success("查询成功").add(userMapper.selectByPhone(userphone));
    }

    /**
     * 绑定用户相关的信息
     * @param record
     * @return
     */
    private MessageUtil bindUser(User record){
        record.setUserInfo(record.getUserId());
        if(userMapper.updateByPrimaryKeySelective(record) > 0){
            UserRole userRole = new UserRole();
            userRole.setUserId(record.getUserId());
            userRole.setRoleId(2);
            Userinfo userinfo = new Userinfo(record.getUserId(), 0);    //走一个默认的性别 男
            return userRoleMapper.insertSelective(userRole) > 0 && userinfoMapper.insertSelective(userinfo) >0 ? MessageUtil.success("注册成功").add(record) : MessageUtil.fail("出现异常请联系管理员").add(record);
        }
        return MessageUtil.fail("绑定用户详细信息失败").add(record);
    }
}
